<template>
  <div class="user-management">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h1 class="page-title">用户管理</h1>
        <p class="page-description">管理系统中的所有用户信息</p>
      </div>
      <div class="header-actions">
        <button class="btn btn-success" @click="addUser">
          <i class="fas fa-plus"></i>
          新增用户
        </button>
        <button class="btn btn-primary" @click="refreshUsers">
          <i class="fas fa-refresh"></i>
          刷新
        </button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-section">
      <div class="stats-grid">
        <div class="stat-card active">
          <div class="stat-icon">
            <i class="fas fa-user-check"></i>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.active }}</div>
            <div class="stat-label">正常用户</div>
          </div>
        </div>
        
        <div class="stat-card inactive">
          <div class="stat-icon">
            <i class="fas fa-user-slash"></i>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.inactive }}</div>
            <div class="stat-label">已禁用</div>
          </div>
        </div>
        
        <div class="stat-card total">
          <div class="stat-icon">
            <i class="fas fa-users"></i>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.total }}</div>
            <div class="stat-label">总用户数</div>
          </div>
        </div>
        
        <div class="stat-card new">
          <div class="stat-icon">
            <i class="fas fa-user-plus"></i>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.newToday }}</div>
            <div class="stat-label">今日新增</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索和过滤 -->
    <div class="filters-section">
      <div class="search-box">
        <div class="search-input-wrapper">
          <i class="fas fa-search search-icon"></i>
          <input
            v-model="searchQuery"
            type="text"
            placeholder="搜索用户名、邮箱、手机号..."
            class="search-input"
          @input="handleSearch"
          />
          <button
            v-if="searchQuery"
            class="clear-search"
            @click="clearSearch"
          >
            <i class="fas fa-times"></i>
          </button>
      </div>
    </div>

      <div class="filter-controls">
        <select v-model="statusFilter" @change="applyFilters" class="filter-select">
          <option value="">全部状态</option>
          <option value="1">正常用户</option>
          <option value="0">已禁用</option>
        </select>
      </div>
    </div>

    <!-- 悬浮批量操作按钮 -->
    <div v-if="selectedUsers.length > 0" class="floating-batch-actions">
      <div class="batch-main">
      <div class="batch-info">
          <span class="selected-count">{{ selectedUsers.length }}</span>
          <span class="selected-text">已选择</span>
      </div>
      <div class="batch-buttons">
          <button @click="showBatchMenu = !showBatchMenu" class="batch-toggle-btn" :class="{ active: showBatchMenu }">
            <i class="fas" :class="showBatchMenu ? 'fa-times' : 'fa-cog'"></i>
          </button>
      </div>
    </div>

      <!-- 批量操作菜单 -->
      <div v-if="showBatchMenu" class="batch-menu">
        <div class="batch-menu-header">
          <h4>批量操作</h4>
        </div>
        <div class="batch-menu-content">
          <div class="batch-action-group">
            <label class="action-label">状态更新</label>
            <div class="action-row">
              <select v-model="batchStatusUpdate" class="batch-status-select">
                <option value="">选择新状态</option>
                <option value="1">启用用户</option>
                <option value="0">禁用用户</option>
              </select>
              <button @click="batchUpdateUserStatus" class="btn btn-primary btn-sm" :disabled="!batchStatusUpdate">
                <i class="fas fa-edit"></i>
                更新状态
              </button>
            </div>
          </div>
          
          <div class="batch-action-group">
            <label class="action-label">危险操作</label>
            <div class="action-row">
              <button @click="batchDeleteUsers" class="btn btn-danger btn-sm btn-full">
                <i class="fas fa-trash"></i>
                批量删除用户
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 用户列表 -->
    <div class="users-section">
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <span>加载用户数据中...</span>
      </div>
      
      <div v-else-if="filteredUsers.length === 0" class="empty-state">
        <i class="fas fa-users"></i>
        <h3>{{ searchQuery || statusFilter ? '未找到匹配的用户' : '暂无用户数据' }}</h3>
        <p>{{ searchQuery || statusFilter ? '请尝试调整搜索条件' : '系统中还没有注册用户' }}</p>
      </div>
      
      <div v-else class="users-table-container">
        <table class="users-table">
          <thead>
            <tr>
              <th class="checkbox-column">
                <input
                  type="checkbox"
                  :checked="isAllSelected"
                  @change="toggleSelectAll"
                  class="batch-checkbox"
                />
              </th>
              <th>用户信息</th>
              <th>联系方式</th>
              <th>注册时间</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr
              v-for="user in paginatedUsers"
              :key="user.userId"
              class="user-row"
            >
              <td class="checkbox-column">
                <input
                  type="checkbox"
                  :value="user.userId"
                  v-model="selectedUsers"
                  class="batch-checkbox"
                />
              </td>
              
              <td class="user-info-cell">
                <div class="user-avatar">
                  <i class="fas fa-user"></i>
                </div>
                <div class="user-details">
                  <div class="user-name">{{ user.username }}</div>
                  <div class="user-id">ID: {{ user.userId }}</div>
                </div>
              </td>
              
              <td class="contact-cell">
                <div class="contact-info">
                  <div class="contact-item">
                    <i class="fas fa-envelope"></i>
                    <span>{{ user.email || '-' }}</span>
                  </div>
                  <div class="contact-item">
                    <i class="fas fa-phone"></i>
                    <span>{{ user.phoneNumber || '-' }}</span>
                  </div>
                </div>
              </td>
              
              <td class="date-cell">
                {{ formatDate(user.createdAt) }}
              </td>
              
              <td class="status-cell">
                <span
                  class="status-badge"
                  :class="getUserStatusClass(user.status)"
                >
                  {{ getUserStatusText(user.status) }}
                </span>
              </td>
              
              <td class="actions-cell">
                <div class="action-buttons">
                  <button
                    class="btn btn-sm btn-info"
                    @click="viewUserDetails(user)"
                    title="查看详情"
                  >
                    <i class="fas fa-eye"></i>
                  </button>
                  <button
                    class="btn btn-sm btn-primary"
                    @click="editUser(user)"
                    title="编辑用户"
                  >
                    <i class="fas fa-edit"></i>
                  </button>
                  <div class="dropdown">
                    <button
                      class="btn btn-sm btn-secondary dropdown-toggle"
                      @click="toggleDropdown(user.userId)"
                      title="更多操作"
                    >
                      <i class="fas fa-ellipsis-v"></i>
                    </button>
                    <div v-if="activeDropdown === user.userId" class="dropdown-menu">
                      <button
                        class="dropdown-item"
                        @click="toggleUserStatus(user)"
                        :disabled="updatingUserId === user.userId"
                      >
                        <i
                          class="fas"
                          :class="updatingUserId === user.userId ? 'fa-spinner fa-spin' : (user.status === 1 ? 'fa-ban' : 'fa-check')"
                        ></i>
                        {{ user.status === 1 ? '禁用用户' : '启用用户' }}
                      </button>
                      <button class="dropdown-item dropdown-item-danger" @click="deleteUser(user)">
                        <i class="fas fa-trash"></i>
                        删除用户
                      </button>
      </div>
                  </div>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
    </div>

      <!-- 分页 -->
      <div v-if="totalPages > 1" class="pagination-container">
        <div class="pagination-info">
          显示 {{ (currentPage - 1) * pageSize + 1 }} - {{ Math.min(currentPage * pageSize, filteredUsers.length) }} 
          共 {{ filteredUsers.length }} 条记录
      </div>
        <div class="pagination-controls">
          <button
            class="btn btn-sm btn-secondary"
            @click="goToPage(currentPage - 1)"
            :disabled="currentPage === 1"
          >
            <i class="fas fa-chevron-left"></i>
          </button>
          
          <div class="page-numbers">
            <button
              v-for="page in visiblePages"
              :key="page"
              class="btn btn-sm"
              :class="page === currentPage ? 'btn-primary' : 'btn-secondary'"
              @click="goToPage(page)"
            >
              {{ page }}
            </button>
          </div>
          
          <button
            class="btn btn-sm btn-secondary"
            @click="goToPage(currentPage + 1)"
            :disabled="currentPage === totalPages"
          >
            <i class="fas fa-chevron-right"></i>
          </button>
        </div>
      </div>
    </div>

    <!-- 用户详情模态框 -->
    <div v-if="showUserModal" class="modal-overlay" @click="closeUserModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>用户详情</h3>
          <button class="modal-close" @click="closeUserModal">
            <i class="fas fa-times"></i>
          </button>
        </div>
        
        <div class="modal-body">
          <div v-if="selectedUser" class="user-detail-grid">
            <div class="detail-section">
              <h4>基本信息</h4>
              <div class="detail-item">
                <label>用户ID:</label>
                <span>{{ selectedUser.userId }}</span>
              </div>
              <div class="detail-item">
                <label>用户名:</label>
                <span>{{ selectedUser.username }}</span>
              </div>
              <div class="detail-item">
                <label>邮箱:</label>
                <span>{{ selectedUser.email || '-' }}</span>
              </div>
              <div class="detail-item">
                <label>手机号:</label>
                <span>{{ selectedUser.phoneNumber || '-' }}</span>
              </div>
            </div>
            
            <div class="detail-section">
              <h4>账户状态</h4>
              <div class="detail-item">
                <label>状态:</label>
                <span
                  class="status-badge"
                  :class="getUserStatusClass(selectedUser.status)"
                >
                  {{ getUserStatusText(selectedUser.status) }}
                </span>
              </div>
              <div class="detail-item">
                <label>注册时间:</label>
                <span>{{ formatDate(selectedUser.createdAt) }}</span>
              </div>
              <div class="detail-item">
                <label>最后更新:</label>
                <span>{{ formatDate(selectedUser.updatedAt) }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="closeUserModal">
            关闭
          </button>
          <button class="btn btn-primary" @click="editUser(selectedUser)">
            <i class="fas fa-edit"></i>
            编辑用户
          </button>
        </div>
      </div>
    </div>

    <!-- 编辑用户模态框 -->
    <div v-if="showEditModal" class="modal-overlay" @click="closeEditModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ editingUser.userId ? '编辑用户' : '新增用户' }}</h3>
          <button class="modal-close" @click="closeEditModal">
            <i class="fas fa-times"></i>
          </button>
        </div>
        
        <div class="modal-body">
          <form @submit.prevent="saveUser" class="user-form">
            <div class="form-row">
              <div class="form-group">
                <label class="form-label">用户名 *</label>
                <input
                  v-model="editingUser.username"
                  type="text"
                  class="form-input"
                  placeholder="请输入用户名"
                  required
                />
              </div>
              <div class="form-group">
                <label class="form-label">邮箱</label>
                <input
                  v-model="editingUser.email"
                  type="email"
                  class="form-input"
                  placeholder="请输入邮箱地址"
                />
              </div>
            </div>
            
            <div class="form-row">
              <div class="form-group">
                <label class="form-label">手机号</label>
                <input
                  v-model="editingUser.phoneNumber"
                  type="tel"
                  class="form-input"
                  placeholder="请输入手机号"
                />
              </div>
              <div class="form-group">
                <label class="form-label">状态</label>
                <select v-model="editingUser.status" class="form-select">
                  <option :value="1">正常</option>
                  <option :value="0">已禁用</option>
                </select>
              </div>
            </div>
            
            <div v-if="!editingUser.userId" class="form-row">
              <div class="form-group">
                <label class="form-label">密码 *</label>
                <input
                  v-model="editingUser.password"
                  type="password"
                  class="form-input"
                  placeholder="请输入密码"
                  required
                />
              </div>
              <div class="form-group">
                <label class="form-label">确认密码 *</label>
                <input
                  v-model="editingUser.confirmPassword"
                  type="password"
                  class="form-input"
                  placeholder="请再次输入密码"
                  required
                />
              </div>
            </div>
          </form>
        </div>
        
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="closeEditModal">
            取消
          </button>
          <button 
            class="btn btn-primary" 
            @click="saveUser"
            :disabled="saving"
          >
            <i class="fas" :class="saving ? 'fa-spinner fa-spin' : 'fa-save'"></i>
            {{ saving ? '保存中...' : '保存' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted, watch } from 'vue'
import { userAPI } from '@/api'
import { message, confirm, feedback, batchConfirm } from '@/utils/message'


export default {
  name: 'UserManagement',
  setup() {
    // 响应式数据
    const users = ref([])
    const loading = ref(false)
    const searchQuery = ref('')
    const statusFilter = ref('')
    const currentPage = ref(1)
    const pageSize = ref(10)
    const showUserModal = ref(false)
    const selectedUser = ref(null)
    const updatingUserId = ref(null)
    const selectedUsers = ref([])
    const showBatchMenu = ref(false)
    const batchStatusUpdate = ref('')
    const showEditModal = ref(false)
    const editingUser = ref({})
    const saving = ref(false)
    const activeDropdown = ref(null)
    
    // 统计数据
    const stats = ref({
      active: 0,
      inactive: 0,
      total: 0,
      newToday: 0
    })
    
    // 计算属性
    const filteredUsers = computed(() => {
      let filtered = users.value
      
      // 搜索过滤
      if (searchQuery.value) {
        const query = searchQuery.value.toLowerCase()
        filtered = filtered.filter(user =>
          user.username?.toLowerCase().includes(query) ||
          user.email?.toLowerCase().includes(query) ||
          user.phoneNumber?.includes(query)
        )
      }
      
      // 状态过滤
      if (statusFilter.value !== '') {
        filtered = filtered.filter(user => user.status === parseInt(statusFilter.value))
      }
      
      return filtered
    })
    
    const totalPages = computed(() => {
      return Math.ceil(filteredUsers.value.length / pageSize.value)
    })
    
    const paginatedUsers = computed(() => {
      const start = (currentPage.value - 1) * pageSize.value
      const end = start + pageSize.value
      return filteredUsers.value.slice(start, end)
    })
    
    const visiblePages = computed(() => {
      const pages = []
      const maxVisible = 5
      let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
      let end = Math.min(totalPages.value, start + maxVisible - 1)
      
      if (end - start + 1 < maxVisible) {
        start = Math.max(1, end - maxVisible + 1)
      }
      
      for (let i = start; i <= end; i++) {
        pages.push(i)
      }
      
      return pages
    })
    
    const isAllSelected = computed(() => {
      if (paginatedUsers.value.length === 0) return false
      return paginatedUsers.value.every(user => selectedUsers.value.includes(user.userId))
    })
    
    // 计算统计数据
    const updateStats = () => {
      const activeUsers = users.value.filter(user => user.status === 1)
      const inactiveUsers = users.value.filter(user => user.status === 0)
      const today = new Date().toDateString()
      const newTodayUsers = users.value.filter(user => {
        const userDate = new Date(user.createdAt).toDateString()
        return userDate === today
      })
      
      stats.value = {
        active: activeUsers.length,
        inactive: inactiveUsers.length,
        total: users.value.length,
        newToday: newTodayUsers.length
      }
    }
    
    // 方法
    const loadUsers = async () => {
      loading.value = true
      try {
        const response = await userAPI.getAllUsers()
        if (response.success) {
          users.value = response.users
          updateStats()
        } else {
          console.error('加载用户列表失败:', response.message)
        }
      } catch (error) {
        console.error('加载用户列表错误:', error)
      } finally {
        loading.value = false
      }
    }

    const refreshUsers = () => {
      selectedUsers.value = []
      showBatchMenu.value = false
      loadUsers()
    }
    
    const handleSearch = () => {
      currentPage.value = 1
    }
    
    const clearSearch = () => {
      searchQuery.value = ''
      currentPage.value = 1
    }
    
    const applyFilters = () => {
      currentPage.value = 1
    }
    
    const goToPage = (page) => {
      if (page >= 1 && page <= totalPages.value) {
        currentPage.value = page
      }
    }
    
    const viewUserDetails = (user) => {
      selectedUser.value = user
      showUserModal.value = true
    }
    
    const closeUserModal = () => {
      showUserModal.value = false
      selectedUser.value = null
    }
    
    const editUser = (user) => {
      editingUser.value = {
        userId: user.userId,
        username: user.username,
        email: user.email || '',
        phoneNumber: user.phoneNumber || '',
        status: user.status,
        password: '',
        confirmPassword: ''
      }
      showEditModal.value = true
      showUserModal.value = false
    }
    
    const closeEditModal = () => {
      showEditModal.value = false
      editingUser.value = {}
    }
    
    const addUser = () => {
      editingUser.value = {
        userId: null,
        username: '',
        email: '',
        phoneNumber: '',
        status: 1,
        password: '',
        confirmPassword: ''
      }
      showEditModal.value = true
    }
    
    const saveUser = async () => {
      if (!editingUser.value.username) {
        message.warning('请输入用户名')
        return
      }
      
      if (!editingUser.value.userId) {
        // 新增用户
        if (!editingUser.value.password) {
          message.error('请输入密码')
          return
        }
        if (editingUser.value.password !== editingUser.value.confirmPassword) {
          message.error('两次输入的密码不一致')
          return
        }
      }
      
      saving.value = true
      try {
        let response
        if (editingUser.value.userId) {
          // 编辑用户
          response = await userAPI.updateUser(editingUser.value.userId, {
            username: editingUser.value.username,
            email: editingUser.value.email,
            phoneNumber: editingUser.value.phoneNumber,
            status: parseInt(editingUser.value.status)
          })
        } else {
          // 新增用户
          response = await userAPI.createUser({
            username: editingUser.value.username,
            email: editingUser.value.email,
            phoneNumber: editingUser.value.phoneNumber,
            password: editingUser.value.password,
            status: parseInt(editingUser.value.status)
          })
        }
        
        if (response.success) {
          feedback.success(editingUser.value.userId ? '更新用户信息' : '创建用户')
          closeEditModal()
          await loadUsers()
        } else {
          message.error(response.message || '操作失败')
        }
      } catch (error) {
        console.error('保存用户失败:', error)
        feedback.networkError()
      } finally {
        saving.value = false
      }
    }
    
    const deleteUser = async (user) => {
      try {
        await confirm.delete(`删除用户"${user.username}"`, '此操作不可逆，请谨慎操作！')
        const response = await userAPI.deleteUser(user.userId)
        if (response.success) {
          feedback.success('删除用户')
          await loadUsers()
        } else {
          message.error(response.message || '删除失败')
        }
      } catch (error) {
        if (error === 'cancel') return // 用户取消
        console.error('删除用户失败:', error)
        feedback.networkError()
      }
      activeDropdown.value = null
    }
    
    const toggleDropdown = (userId) => {
      activeDropdown.value = activeDropdown.value === userId ? null : userId
    }
    
    const toggleUserStatus = async (user) => {
      if (updatingUserId.value === user.userId) return
      
      updatingUserId.value = user.userId
      const newStatus = user.status === 1 ? 0 : 1
      
      try {
        const response = await userAPI.updateUserStatus(user.userId, newStatus)
        if (response.success) {
          // 更新本地数据
          user.status = newStatus
          updateStats()
          
          // 如果是在模态框中，也更新选中的用户
          if (selectedUser.value && selectedUser.value.userId === user.userId) {
            selectedUser.value.status = newStatus
          }
        } else {
          console.error('更新用户状态失败:', response.message)
          message.error('更新用户状态失败: ' + response.message)
        }
      } catch (error) {
        console.error('更新用户状态错误:', error)
        feedback.networkError()
      } finally {
        updatingUserId.value = null
      }
    }
    
    const formatDate = (date) => {
      if (!date) return '-'
      return new Date(date).toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      })
    }
    
    const getUserStatusClass = (status) => {
      return status === 1 ? 'status-active' : 'status-inactive'
    }
    
    const getUserStatusText = (status) => {
      return status === 1 ? '正常' : '已禁用'
    }
    
    const toggleSelectAll = () => {
      if (isAllSelected.value) {
        selectedUsers.value = []
      } else {
        selectedUsers.value = paginatedUsers.value.map(user => user.userId)
      }
    }
    
    const batchUpdateUserStatus = async () => {
      const userIds = selectedUsers.value
      if (userIds.length === 0) {
        message.warning('请先选择要操作的用户')
        return
      }

      try {
        await batchConfirm('更新状态', userIds.length, '个用户')
        await userAPI.batchUpdateUserStatus(userIds, parseInt(batchStatusUpdate.value))
        feedback.success('批量更新状态')
        await loadUsers()
        selectedUsers.value = []
        showBatchMenu.value = false
      } catch (error) {
        if (error === 'cancel') return // 用户取消
        console.error('批量更新状态失败:', error)
        feedback.error('批量更新状态')
      }
    }
    
    const batchDeleteUsers = async () => {
      const userIds = selectedUsers.value
      if (userIds.length === 0) {
        message.warning('请先选择要删除的用户')
        return
      }

      try {
        await confirm.delete(`批量删除 ${userIds.length} 个用户`, '此操作不可逆，请谨慎操作！')
        await userAPI.batchDeleteUsers(userIds)
        feedback.deleted(userIds.length)
        await loadUsers()
        selectedUsers.value = []
        showBatchMenu.value = false
      } catch (error) {
        if (error === 'cancel') return // 用户取消
        console.error('批量删除失败:', error)
        feedback.error('批量删除')
      }
    }

    // 监听过滤条件变化，重置页码
    watch([searchQuery, statusFilter], () => {
      currentPage.value = 1
    })

    // 点击外部关闭批量操作菜单
    const handleClickOutside = (event) => {
      if (!event.target.closest('.floating-batch-actions')) {
        showBatchMenu.value = false
      }
    }

    // 生命周期
    onMounted(() => {
      loadUsers()
      document.addEventListener('click', handleClickOutside)
    })

    return {
      users,
      loading,
      searchQuery,
      statusFilter,
      currentPage,
      pageSize,
      showUserModal,
      selectedUser,
      updatingUserId,
      selectedUsers,
      showBatchMenu,
      batchStatusUpdate,
      showEditModal,
      editingUser,
      saving,
      activeDropdown,
      stats,
      filteredUsers,
      totalPages,
      paginatedUsers,
      visiblePages,
      isAllSelected,
      loadUsers,
      refreshUsers,
      handleSearch,
      clearSearch,
      applyFilters,
      goToPage,
      viewUserDetails,
      closeUserModal,
      editUser,
      closeEditModal,
      addUser,
      saveUser,
      deleteUser,
      toggleDropdown,
      toggleUserStatus,
      formatDate,
      getUserStatusClass,
      getUserStatusText,
      toggleSelectAll,
      batchUpdateUserStatus,
      batchDeleteUsers
    }
  }
}
</script>

<style scoped>
.user-management {
  padding: 20px;
  background: #f8f9fa;
  min-height: calc(100vh - 60px);
}

/* 页面头部 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  background: #ffffff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.header-content .page-title {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 8px 0;
}

.page-description {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

/* 统计卡片 */
.stats-section {
  margin-bottom: 24px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.stat-card {
  background: #ffffff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  gap: 16px;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: white;
}

.stat-card.active .stat-icon {
  background: linear-gradient(135deg, #27ae60, #229954);
}

.stat-card.inactive .stat-icon {
  background: linear-gradient(135deg, #e74c3c, #c0392b);
}

.stat-card.total .stat-icon {
  background: linear-gradient(135deg, #3498db, #2980b9);
}

.stat-card.new .stat-icon {
  background: linear-gradient(135deg, #f39c12, #e67e22);
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #7f8c8d;
  font-weight: 500;
}

/* 搜索和过滤 */
.filters-section {
  background: #ffffff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  margin-bottom: 24px;
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.search-box {
  flex: 1;
  min-width: 300px;
}

.search-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  color: #7f8c8d;
  z-index: 1;
}

.search-input {
  width: 100%;
  padding: 10px 40px 10px 40px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  background: #ffffff;
  color: #2c3e50;
  transition: all 0.3s ease;
}

.search-input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.clear-search {
  position: absolute;
  right: 12px;
  background: none;
  border: none;
  color: #7f8c8d;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.clear-search:hover {
  color: #2c3e50;
  background: #ecf0f1;
}

.filter-controls {
  display: flex;
  gap: 12px;
  align-items: center;
}

.filter-select {
  padding: 10px 12px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  background: #ffffff;
  color: #2c3e50;
  cursor: pointer;
  transition: all 0.3s ease;
}

.filter-select:focus {
  outline: none;
  border-color: #3498db;
}

/* 用户列表 */
.users-section {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #7f8c8d;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #ecf0f1;
  border-top: 3px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #7f8c8d;
  text-align: center;
}

.empty-state i {
  font-size: 64px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state h3 {
  margin: 0 0 8px 0;
  color: #2c3e50;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

/* 表格样式 */
.users-table-container {
  overflow-x: auto;
}

.users-table {
  width: 100%;
  border-collapse: collapse;
}

.users-table th {
  background: #f8f9fa;
  padding: 16px 12px;
  text-align: left;
  font-weight: 600;
  color: #000000 !important;
  font-size: 14px;
  border-bottom: 1px solid #f1f3f4;
}

.users-table td {
  padding: 16px 12px;
  border-bottom: 1px solid #f1f3f4;
  vertical-align: top;
  background: #ffffff;
}

.user-row {
  background: #ffffff;
  transition: all 0.3s ease;
}

.user-row:hover {
  background: #f8f9fa;
}

.user-info-cell {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #3498db, #2980b9);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-name {
  font-weight: 500;
  color: #2c3e50;
  font-size: 14px;
}

.user-id {
  font-size: 12px;
  color: #7f8c8d;
}

.contact-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #5a6c7d;
}

.contact-item i {
  width: 14px;
  color: #7f8c8d;
}

.date-cell {
  font-size: 13px;
  color: #5a6c7d;
}

.status-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  border: 1px solid;
}

.status-active {
  background: rgba(39, 174, 96, 0.1);
  color: #229954;
  border-color: rgba(39, 174, 96, 0.3);
}

.status-inactive {
  background: rgba(231, 76, 60, 0.1);
  color: #c0392b;
  border-color: rgba(231, 76, 60, 0.3);
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.checkbox-column {
  width: 50px;
  text-align: center;
  padding: 12px 8px;
}

.checkbox-column input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

/* 分页 */
.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-top: 1px solid #f1f3f4;
}

.pagination-info {
  font-size: 14px;
  color: #7f8c8d;
}

.pagination-controls {
  display: flex;
  gap: 8px;
  align-items: center;
}

.page-numbers {
  display: flex;
  gap: 4px;
}

/* 按钮样式 */
.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: #3498db;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: #2980b9;
}

.btn-secondary {
  background: #ecf0f1;
  color: #7f8c8d;
}

.btn-secondary:hover:not(:disabled) {
  background: #d5dbdb;
}

.btn-info {
  background: #17a2b8;
  color: white;
}

.btn-info:hover:not(:disabled) {
  background: #138496;
}

.btn-success {
  background: #28a745;
  color: white;
}

.btn-success:hover:not(:disabled) {
  background: #218838;
}

.btn-warning {
  background: #ffc107;
  color: #212529;
}

.btn-warning:hover:not(:disabled) {
  background: #e0a800;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  backdrop-filter: blur(5px);
}

.modal-content {
  background: #ffffff;
  border-radius: 16px;
  width: 600px;
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px;
  border-bottom: 1px solid #f1f3f4;
}

.modal-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: #7f8c8d;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.3s ease;
}

.modal-close:hover {
  color: #e74c3c;
}

.modal-body {
  padding: 24px;
}

.user-detail-grid {
  display: grid;
  gap: 24px;
}

.detail-section h4 {
  margin: 0 0 16px 0;
  color: #2c3e50;
  font-size: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid #f1f3f4;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f1f3f4;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-item label {
  font-weight: 500;
  color: #7f8c8d;
  font-size: 14px;
}

.detail-item span {
  color: #2c3e50;
  font-size: 14px;
}

.modal-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding: 24px;
  border-top: 1px solid #f1f3f4;
}

/* 编辑表单样式 */
.user-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.form-row:last-child {
  margin-top: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  font-size: 14px;
  font-weight: 500;
  color: #2c3e50;
}

.form-input,
.form-select {
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  background: #ffffff;
}

.form-input:focus,
.form-select:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.form-input::placeholder {
  color: #95a5a6;
}

/* 下拉菜单样式 */
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-toggle {
  position: relative;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: #ffffff;
  border: 1px solid #e1e5e9;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  z-index: 9999;
  min-width: 160px;
  padding: 8px 0;
  margin-top: 4px;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 16px;
  background: none;
  border: none;
  color: #2c3e50;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: left;
}

.dropdown-item:hover {
  background: #f8f9fa;
  color: #3498db;
}

.dropdown-item-danger:hover {
  background: rgba(231, 76, 60, 0.05);
  color: #c0392b;
}

.dropdown-item:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  color: #95a5a6;
}

.dropdown-item:disabled:hover {
  background: none;
  color: #95a5a6;
}

.dropdown-item i {
  width: 16px;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .filters-section {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }
  
  .search-input-wrapper {
    max-width: none;
  }
  
  .users-table th,
  .users-table td {
    padding: 12px 16px;
  }
  
  .pagination-container {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .pagination-controls {
    justify-content: center;
  }
  
  .modal-content {
    width: 95vw;
    margin: 20px;
  }
}

 /* 悬浮批量操作按钮 */
 .floating-batch-actions {
   position: fixed;
   bottom: 30px;
   right: 30px;
   background: #ffffff;
   border-radius: 16px;
   box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
   z-index: 1000;
   border: 1px solid #e9ecef;
   overflow: hidden;
   transition: all 0.3s ease;
 }
 
 .batch-main {
   display: flex;
   align-items: center;
   gap: 12px;
   padding: 16px 20px;
 }
 
 .batch-info {
   display: flex;
   align-items: center;
   gap: 8px;
 }
 
 .selected-count {
   font-size: 18px;
   font-weight: 700;
   color: #3498db;
   background: #ecf4fd;
   padding: 4px 8px;
   border-radius: 8px;
   min-width: 28px;
   text-align: center;
 }
 
 .selected-text {
   font-size: 14px;
   color: #7f8c8d;
   font-weight: 500;
 }
 
 .batch-buttons {
   position: relative;
 }
 
 .batch-toggle-btn {
   width: 36px;
   height: 36px;
   border-radius: 50%;
   background: #3498db;
   color: white;
   border: none;
   cursor: pointer;
   display: flex;
   align-items: center;
   justify-content: center;
   font-size: 14px;
   transition: all 0.3s ease;
 }
 
 .batch-toggle-btn:hover {
   background: #2980b9;
   transform: scale(1.05);
 }
 
 .batch-toggle-btn.active {
   background: #e74c3c;
   transform: rotate(90deg);
 }
 
 .batch-menu {
   border-top: 1px solid #f1f3f4;
   background: #fafbfc;
 }
 
 .batch-menu-header {
   padding: 12px 20px;
   border-bottom: 1px solid #f1f3f4;
   background: #ffffff;
 }
 
 .batch-menu-header h4 {
   margin: 0;
   font-size: 14px;
   font-weight: 600;
   color: #2c3e50;
 }
 
 .batch-menu-content {
   display: flex;
   flex-direction: column;
   gap: 16px;
   padding: 16px 20px;
 }
 
 .batch-action-group {
   display: flex;
   flex-direction: column;
   gap: 8px;
 }
 
 .action-label {
   font-size: 12px;
   font-weight: 600;
   color: #7f8c8d;
   text-transform: uppercase;
   letter-spacing: 0.5px;
 }
 
 .action-row {
   display: flex;
   align-items: center;
   gap: 8px;
 }
 
 .batch-status-select {
   padding: 8px 12px;
   border: 1px solid #ddd;
   border-radius: 6px;
   font-size: 12px;
   flex: 1;
   min-width: 120px;
   background: white;
 }
 
 .batch-status-select:focus {
   outline: none;
   border-color: #3498db;
 }
 
 .btn-full {
   width: 100%;
   justify-content: center;
 }

/* 额外的表格样式 */
.users-table {
  border-collapse: collapse;
}

.checkbox-column {
  width: 24px;
  text-align: center;
}

.checkbox-column input {
  width: 16px;
  height: 16px;
  cursor: pointer;
}
</style> 